<template>
  <div class="RobotAlarmQuery">
    <div class="filter">
      <el-form ref="form" label-width="90px">
        <div class="operation">
          <el-form-item label>
            <el-radio-group v-model="queryForm.checkStatus">
              <el-radio :label="0">未审核</el-radio>
              <el-radio :label="1">已审核</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="开始时间:">
            <el-date-picker
              v-model="queryForm.startTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期时间"
              :picker-options="startPickerOptions"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间:">
            <el-date-picker
              v-model="queryForm.endTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期时间"
              :picker-options="endPickerOptions"
            ></el-date-picker>
          </el-form-item>
          <el-button class="but-query" type="text" size="medium" @click="queryClick">
            <i class="query-icon"></i>查询
          </el-button>
          <el-button class="but-audit" type="text" size="medium" @click="examineClick">
            <i class="audit-icon"></i>审核
          </el-button>
        </div>
      </el-form>
    </div>
    <div class="content">
      <div class="content-head">用户操作</div>
      <div class="content-list">
        <el-table
          :data="tableData"
          border
          stripe
          size="mini"
          class="el-table-redefine"
          style="width: 100%;"
          height="100%"
          @select="handleSelect"
          @selection-change="handleSelectionChange"
          @row-click="handleClick"
        >
          <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
          <el-table-column type="selection" align="center" width="50" :selectable="isSelectable"></el-table-column>
          <el-table-column prop="checkStatus" label="审核状态">
            <template slot-scope="scope">
              <span v-if="scope.row.checkStatus === 0">未审核</span>
              <span v-if="scope.row.checkStatus === 1">已审核</span>
            </template>
          </el-table-column>
          <el-table-column prop="alarmTypeId" label="报警类型">
            <template slot-scope="scope">
              <span v-if="scope.row.alarmTypeId === 0">故障报警</span>
              <span v-if="scope.row.alarmTypeId === 1">越限报警</span>
              <span v-if="scope.row.alarmTypeId === 2">温升报警</span>
            </template>
          </el-table-column>
          <el-table-column prop="alarmLevel" label="报警级别">
            <template slot-scope="scope">
              <span v-if="scope.row.alarmLevel === 0">正常</span>
              <span v-if="scope.row.alarmLevel === 1">预警</span>
              <span v-if="scope.row.alarmLevel === 2">一般告警</span>
              <span v-if="scope.row.alarmLevel === 3">严重告警</span>
              <span v-if="scope.row.alarmLevel === 4">危急告警</span>
            </template>
          </el-table-column>
          <el-table-column prop="robot.name" label="机器人名称">
            <template slot-scope="scope">
              <span>{{scope.row.robot.name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="sysPoint.displayName" label="报警内容">
            <template slot-scope="scope">
              <span>{{scope.row.sysPoint.displayName}} {{scope.row.sysPoint.description}}! 错误代码:{{scope.row.sysPoint.name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="报警时间">
            <template slot-scope="scope">
              <span>{{scope.row.createTime}}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-area">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper, slot"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[20, 50, 100, 300]"
          :page-size="queryForm.size"
          :current-page="queryForm.page"
          :total="tansksTotal"
        >
          <el-button type="text" size="mini">前往</el-button>
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { dateToDateStr, getDateTopMonth } from "@/utils/utilsMethod";
import {
  sysPointAlarmHistorysList,
  sysPointAlarmHistorysConfirm,
  postOperationLogs
} from "@/api/apiManagement";

export default {
  data() {
    return {
      robot_id: null,
      queryForm: {
        page: 1,
        size: 20,
        robotId: null,
        startTime: "",
        endTime: "",
        checkStatus: 0,
        alarmLevel: ""
      },
      tansksTotal: 0,
      tableData: [],
      tableSelection: [],
      startPickerOptions: {
        disabledDate: time => {
          let endTime =
            this.queryForm.endTime === null
              ? ""
              : this.queryForm.endTime;
          return (
            time.getTime() > new Date(endTime).getTime() ||
            time.getTime() > Date.now() - 8.64e6
          );
        }
      },
      endPickerOptions: {
        disabledDate: time => {
          let startTime =
            this.queryForm.startTime === null
              ? ""
              : this.queryForm.startTime;
          return (
            time.getTime() <
              new Date(startTime).getTime() - 8 * 60 * 60 * 1000 ||
            time.getTime() > Date.now() - 8.64e6
          ); //如果没有后面的-8.64e6就是不可以选择今天的   减去一天的时间代表可以选择同一天
        }
      }
    };
  },
  created() {
    this.robot_id = localStorage.getItem("ydRobot_id");
    //
    this.queryForm.startTime = dateToDateStr(getDateTopMonth(new Date()));
    this.queryForm.endTime = dateToDateStr(new Date());
    this.queryForm.robotId = this.robot_id;
    //
    this.getSysPointAlarmHistorysList(this.queryForm);
  },
  mounted() {},
  methods: {
    queryClick() {
      this.getSysPointAlarmHistorysList(this.queryForm);
    },
    examineClick() {
      if (this.tableSelection && this.tableSelection.length > 0) {
        var strIds = this.tableSelection
          .map(item => {
            return item.id;
          })
          .join(",");
        this.setSysPointAlarmHistorysConfirm({ ids: strIds });
      } else {
        this.$message({
          message: "请选择告警信息后审核!"
        });
      }
    },
    isSelectable(row, index) {
      if (row.checkStatus === 0) {
        return true;
      }
      return false;
    },
    handleSelect(selection, row) {
      //
    },
    handleSelectionChange(val) {
      this.tableSelection = val;
    },
    handleClick(row, column, event) {
      //
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.getSysPointAlarmHistorysList(this.queryForm);
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.getSysPointAlarmHistorysList(this.queryForm);
    },
    //
    getSysPointAlarmHistorysList(param) {
      param.startTime = param.startTime == null ? "" : param.startTime;
      param.endTime =
        param.endTime == null
          ? ""
          : param.endTime.length <= 10 && param.endTime.length != 0
          ? param.endTime + " 24:00:00"
          : param.endTime;
      sysPointAlarmHistorysList(param)
        .then(response => {
          this.tansksTotal = response.total;
          if (response.list) {
            this.tableData = response.list;
          }
        })
        .catch(() => {});
    },
    //
    setSysPointAlarmHistorysConfirm(param) {
      sysPointAlarmHistorysConfirm(param)
        .then(response => {
          this.tableSelection.forEach(item => {
            this.setOperationLogs(
              "审核-机器人告警查询-" + item.sysPoint.displayName
            );
          });
          this.getSysPointAlarmHistorysList(this.queryForm);
          this.$message({
            type: "success",
            message: "审核成功"
          });
        })
        .catch(() => {});
    },
    // 添加日志管理
    setOperationLogs(content) {
      var parm = {
        content: content,
        deviceId: localStorage.getItem("ydRobot_device_id"),
        type: 0
      };
      postOperationLogs(parm)
        .then(response => {})
        .catch(() => {});
    }
  }
};
</script>

<style>
.RobotAlarmQuery .filter .el-form-item {
  margin-top: 0;
  margin-bottom: 3px;
}
.RobotAlarmQuery .filter .el-form-item__label {
  font-size: 13px;
  line-height: 26px;
}
.RobotAlarmQuery .filter .el-form-item__content {
  font-size: 13px;
  line-height: 26px;
}
.RobotAlarmQuery .filter .el-radio__label {
  font-size: 13px;
}
.RobotAlarmQuery .filter .el-radio {
  width: 80px;
  margin-left: 20px;
  margin-right: 10px;
}
.RobotAlarmQuery .filter .el-radio + .el-radio {
  margin-left: 10px;
}
.RobotAlarmQuery .filter .el-checkbox {
  font-size: 13px;
}
.RobotAlarmQuery .filter .el-checkbox__label {
  font-size: 13px;
}
.RobotAlarmQuery .filter .el-checkbox {
  width: 100px;
  margin-left: 10px;
}
.RobotAlarmQuery .filter .el-checkbox + .el-checkbox {
  margin-left: 10px;
}
/*  */

.RobotAlarmQuery {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column;
}
.RobotAlarmQuery .filter {
  position: relative;
  width: 100%;
  /* height: auto; */
  height: 30px;
  background-color: #eeeeee;
}
.RobotAlarmQuery .operation {
  width: 100%;
  height: 30px;
  background: linear-gradient(
    rgb(227, 242, 238),
    rgb(202, 231, 238)
  ); /* 标准的语法 */
  border-top: 1px solid rgb(202, 231, 238);
}

.RobotAlarmQuery .operation .el-form-item {
  margin-top: 1px;
  margin-bottom: 0;
  float: left;
}

.RobotAlarmQuery .operation .el-form-item__content {
  width: 230px;
  margin-left: 5px !important;
  line-height: 26px;
  float: left;
}
.RobotAlarmQuery .operation .el-date-editor.el-input {
  width: 200px;
}
.RobotAlarmQuery .operation .el-input__inner {
  height: 26px;
  line-height: 26px;
}
.RobotAlarmQuery .operation .el-input__icon {
  line-height: 26px;
}
.RobotAlarmQuery .but-query,
.RobotAlarmQuery .but-audit {
  height: 30px;
  margin-left: 20px;
  margin-right: 10px;
  padding: 0;
  font-size: 13px;
  color: #000;
  float: left;
}
.RobotAlarmQuery .query-icon {
  width: 14px;
  height: 14px;
  margin-right: 3px;
  background-image: url(../../../static/resources/images/query.png);
  background-size: 100% 100%;
  float: left;
}
.RobotAlarmQuery .audit-icon {
  width: 14px;
  height: 14px;
  margin-right: 3px;
  background-image: url(../../../static/resources/images/modify.png);
  background-size: 100% 100%;
  float: left;
}

.RobotAlarmQuery .content {
  position: relative;
  width: 100%;
  flex-grow: 1;
}
.RobotAlarmQuery .content-head {
  width: 100%;
  height: 27px;
  padding: 0 5px;
  line-height: 27px;
  font-size: 13px;
  background: linear-gradient(
    rgb(227, 242, 238),
    rgb(202, 231, 238)
  ); /* 标准的语法 */
}

.RobotAlarmQuery .content-list {
  position: absolute;
  width: 100%;
  height: calc(100% - 59px);
  /* overflow: auto; */
}

.RobotAlarmQuery .el-table-redefine {
  width: 100%;
  height: 100%;
  /* display: flex;
  flex-flow: column; */
}

.RobotAlarmQuery .el-table-redefine .el-table__body-wrapper {
  /* overflow: hidden;
  overflow-y: auto;
  flex-grow: 1; */
}

.RobotAlarmQuery .pagination-area {
  position: absolute;
  width: 100%;
  height: 32px;
  bottom: 0px;
  background-color: #eeeeee;
}
</style>